In This Part
by Bill Heyman
In This Chapter
Using MFCs CFile class to handle normal file I/O operations is the most common way to store your application data. In many applications, however, simple file I/O operations are not enough. For example, if your application needs fast access to data elements matching specific criteria, you can use a relational database to access your organized data. MFC supports relational databases via a number of interfaces: ODBC (open database connectivity), DAO (Data Access Objects), OLE DB, and ADO (ActiveX Data Objects). Each interface represents an evolutionary stage in Microsofts development of programmatic database support: ODBC is the tried-and-true and ADO is the up-and-coming.
This chapter and Chapter 19, Advanced Database Support, show you how to access relational databases from your MFC application. This chapter starts with an introduction to relational database concepts (with an MFC accent) and finishes with a description of ODBC and DAO interfaces. If you are already familiar with relational database concepts, feel free to skim ahead to the MFC specifics in the later part of the chapter.
A database provides a way to group and organize your data logically. In general, a database is nothing more than a structured file. To access the information in this structured file, you use an interface that understands how to manipulate the data within the structured file.
Although there are different types of databases (Indexed Sequential Access Method (ISAM), relational, and object-oriented, to name a few), the majority of the worlds data that is stored in databases is in relational databases. A relational database provides a means of storing data in logical groupings of similar data items. In addition, these data items can reference other logical groupings of other similar data items, which are the actual relations.
A relational database contains tables, columns, and records. In addition, it can support cursors and transactions. Furthermore, most relational databases support a language for interacting with the information contained within thema language called SQL (Structured Query Language).
Note:Most people spell out S-Q-L when they speak in reference to the SQL language. However, you might run into some folks who say sequel, as in Sybase (and Microsofts) SQL (sequel) Server. Both pronunciations are referencing the same language, so feel free to treat both pronunciations as referring to the same thing.
The key entity within a relational database is a table. Use tables to group your data logically. For example, in the TechBooks sample database provided with this chapter, there are several tables: Books, Authors, Publishers, Topics, Categories, and BookAuthors, as shown in Figure 18.1. Each table is designed to store data of a specific type. Thus, the Books table contains all the book information, the Authors table contains all the author information, and the BookAuthors table matches (relates) each entry in the Books table to one or more entries in the Authors table.
Figure 18.1 The tables in the TechBooks database.
The database contains fields for each logical grouping of data within your table. These fields are called columns. Figure 18.2 demonstrates the columns within the Books table in the TechBooks database. Observe that the Books table contains columns for each data element that you would expect to be associated with a book, including title, ISBN, and retail price. Each column has a datatype and size associated with it. For example, the title column is text type and limited to 150 characters, whereas the retail price column is currency type and supports two digits past the decimal point.
So far, Ive discussed only the meta information about the datathat is, the tables and columns provide a general description of the data, but not the data itself. The data itself, as stored in each table, is called that tables records. For example, each book in the Books database has its own record. The set of all books in the TechBooks database is called the records in the Books table. Figure 18.3 shows the records in the TechBooks database.
Figure 18.2 The columns in the Books table.
Figure 18.3 The data records in the Books table.
Note:Records are also called rows. In relational database terminology, records and rows are equivalent.
|
How Do You Define the Structure of Your Data into Relational Database Tables and Columns? Depending on the data itself, this question can be answered simply or with much difficulty. Indeed, some software engineers have dedicated their careers to the science and art of relational database design. (Therefore, dont expect anything near a complete answer in this book.) In particular, one goal is to eliminate redundancy in your database. In the TechBooks database, for example, you might have noticed that authors were not included in the Books table, even though an author appears to be a logical element associated with a book. Instead, a new table called BookAuthors was created to store these relationships. The primary reason for this was to eliminate redundancyspecifically, the Books table does not need duplicate records for a single book when it has multiple authors. Nor does the Books table need a fixed number of columns to support multiple authors. Likewise, the Authors table maps the authors name as a text string to a unique number, the AuthorId. This number is stored in the BookAuthors Author column, rather than the string itself. This approach saves disk space and provides rapid lookup. The science of squeezing the redundancy out of relational databases is called normalization. |
When you have tables, columns, and records, the next logical step is to look at and modify the data in your tables columns. MFC defines database interfaces around the concept of a cursor.
A cursor represents a current record in a table. If you are programmatically scanning the records in your table, your program can look at each record, one at a time. The cursor contains an internal reference to the row that your program is visiting. The MFC CRecordSet and CDaoRecordSet classes provide interfaces for your application to move forward and backward through table records.